<?php
/** @noinspection ALL */
declare (strict_types=1);

namespace app\common\service;

use app\common\logic\advert\AdvertLink;
use app\common\logic\Template;
use app\course\logic\base\Course;
use app\school\logic\school\School;
use app\user\logic\teacher\Teacher;
use think\annotation\Inject;
use think\facade\Event;
use think\facade\Request;
use think\Response;

/**
 * 首页接口类
 */
class Home extends BaseService
{
    protected $is_pc = false;

    /**
     * @Inject()
     *
     * @var Course
     */
    protected $courseLogic;

    /**
     * @Inject()
     *
     * @var Teacher
     */
    protected $teacherLogic;

    public function __construct()
    {
        $this->is_pc = request()->is_pc;
    }

    // 首页接口类
    public function index(): Response
    {
        // 首页顺序后台配置
        // ['key' => 'recommendSchool', 'title' => '推荐机构'],
        // ['key' => 'categoryCourse', 'title' => '分类课程'],

        Event::trigger('WebsiteVisit', [
            'mhm_id'        => 1,
            'resource_type' => 'homepage',
            'resource_id'   => 0,
        ]);

        $home[] = ['key' => 'banner', 'title' => '轮播图', 'list' => $this->getBanner()];
        $home[] = ['key' => 'advert', 'title' => '广告位', 'list' => $this->getAdvert()];
        $home[] = ['key' => 'favoriteCourse', 'title' => $this->getFavoriteTitle(), 'list' => $this->getFavoriteCourse()];
        $home[] = ['key' => 'flashSaleCourse', 'title' => '限时打折', 'list' => $this->getFlashSaleCourse()];
        $home[] = ['key' => 'recommendCourse', 'title' => '推荐课程', 'list' => $this->getRecommendCourse()];
        if (!$this->is_pc) $home[] = ['key' => 'recommendWellSale', 'title' => '好课热销', 'list' => $this->getRecommendWellSale()];
        $home[] = ['key' => 'recommendTeacher', 'title' => '优秀讲师', 'list' => $this->getRecommendTeacher()];

        return static::parseToData($home, 1, '获取成功');
    }

    // 获取首页banner
    public function getBanner()
    {
        $key = $this->is_pc ? 'pc.index' : 'index';
        return app(AdvertLink::class)->getLinkByKey($key);
    }

    // 获取首页广告
    public function getAdvert()
    {
        $key = $this->is_pc ? 'pc.advert' : 'advert';
        return app(AdvertLink::class)->getLinkByKey($key);
    }

    // 获取意向课程
    public function getFavoriteTitle()
    {
        return $this->courseLogic->getFavoriteTitle();
    }

    // 获取意向课程
    public function getFavoriteCourse()
    {
        return $this->courseLogic->getFavoriteCourse();
    }

    // 获取限时打折课程
    public function getFlashSaleCourse()
    {
        return $this->courseLogic->getFlashSaleCourse();
    }

    // 获取首页推荐讲师
    public function getRecommendTeacher()
    {
        return $this->teacherLogic->getRecommend();
    }

    // 获取首页推荐机构
    public function getRecommendSchool()
    {
        return app(School::class)->getRecommend();
    }

    // 获取首页推荐课程
    public function getRecommendCourse(): array
    {
        if (!$this->is_pc) return $this->courseLogic->getRecommend();
        return [
            'recommendCourse'   => $this->courseLogic->getRecommend(),
            'recommendWellSale' => $this->courseLogic->getRecommendWellSale()
        ];
    }

    public function getRecommendWellSale(): array
    {
        return $this->courseLogic->getRecommendWellSale();
    }

    // 获取首页分类课程
    public function getCategoryCourse()
    {
        return app(Course::class)->getCategoryCourse();
    }

    /**
     * template
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/07/11
     *
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function template()
    {
        $temp = app(Template::class)->getTemplateData();
        return static::parseToData($temp, 1, '获取成功');
    }

    /**
     * cateCourse
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/07/11
     *
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cateCourse()
    {
        $cate = Request::get('cate/d');
        $temp = app(Template::class)->getTemplateCateData($cate);
        return static::parseToData($temp, 1, '获取成功');
    }

}
